package ch03_singleton.lazy;

import java.io.Serializable;

/**
 * 1、基础写法，懒汉式单例，线程不安全
 *
 * @author guod
 * @version 1.0
 */
public class LazySingleton implements Serializable {
    // 01、静态属性对象
    private static LazySingleton car = null;

    // 02、构造器私有
    private LazySingleton() {
    }

    // 03通过方法返回01的对象（静态工厂方法）
    public static LazySingleton getInstance() {
        // 线程不安全
        if (car == null) {
            car = new LazySingleton();
        }
        return car;
    }

    // 多线程debug，进行线程干预
    public static void main(String[] args) {
        Thread t01 = new Thread(() -> {
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(Thread.currentThread().getName() + " " + instance);
        });
        Thread t02 = new Thread(() -> {
            LazySingleton instance = LazySingleton.getInstance();
            System.out.println(Thread.currentThread().getName() + " " + instance);
        });
        t01.start();
        t02.start();
    }
}
